package database

import (
	"WIKIHepler_PCR/logger"
	"fmt"
)

func init() {
	link()
	defer close()

	sql_table := `
	CREATE TABLE IF NOT EXISTS "event_calendar"(
		"e_id" INTEGER PRIMARY KEY AUTOINCREMENT,
		"e_name" VARCHAR(255) NOT NULL,
		"e_start" INTEGER NOT NULL,
		"e_end" INTEGER NOT NULL,
		"e_hash" VARCHAR(255) UNIQUE NOT NULL
		)`

	_, err := db.Exec(sql_table)

	if err != nil {
		logger.GetLogger().PrintWarn("Can not create table because of  : " + err.Error())
	}
}

//增加单条活动
func EVENT_AddOne(obj EVENT) (aft int64) {
	link()
	defer close()

	sql_insert := `
	INSERT INTO event_calendar
	(e_name,e_start,e_end,e_hash) VALUES
	(?,?,?,?)`

	stmt, err := db.Prepare(sql_insert)
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
		return 0
	}

	res, err := stmt.Exec(obj.E_name, obj.E_start, obj.E_end, obj.HASHCODE())
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
		return 0
	}
	aft, _ = res.RowsAffected()
	return
}

//获取所有活动
func EVENT_GetAll() (objs []*EVENT, err error) {
	link()
	defer close()

	sql_query := `
	SELECT e_id, e_name, e_start, e_end  FROM "event_calendar"
	`

	rows, err := db.Query(sql_query)
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
		return nil, err
	}

	for rows.Next() {
		var e EVENT
		err := rows.Scan(&e.E_id, &e.E_name, &e.E_start, &e.E_end)
		if err != nil {
			logger.GetLogger().PrintWarn(err.Error())
			return nil, err
		} else {
			objs = append(objs, &e)
		}
	}
	return objs, nil
}

//分页查询活动
func EVENT_Page(page int, pagesize int) (objs []*EVENT) {
	link()
	defer close()

	sql_query := fmt.Sprintf(`
	SELECT e_id, e_name, e_start, e_end
	FROM event_calendar ORDER BY e_id LIMIT %d , %d;
	`, (page-1)*pagesize, pagesize)

	rows, err := db.Query(sql_query)
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
	}

	for rows.Next() {
		var e EVENT
		err := rows.Scan(&e.E_id, &e.E_name, &e.E_start, &e.E_end)
		if err != nil {
			logger.GetLogger().PrintWarn(err.Error())
		} else {
			objs = append(objs, &e)
		}
	}
	return

}

//获取最新的活动日期
func EVENT_GetNewest(endTime int64) (objs []*EVENT) {
	link()
	defer close()

	sql_query := fmt.Sprintf(`
	SELECT e_id, e_name, e_start, e_end
	FROM event_calendar WHERE e_end >= %d ORDER BY e_id ;
	`, endTime)

	rows, err := db.Query(sql_query)
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
	}

	for rows.Next() {
		var e EVENT
		err := rows.Scan(&e.E_id, &e.E_name, &e.E_start, &e.E_end)
		if err != nil {
			logger.GetLogger().PrintWarn(err.Error())
		} else {
			objs = append(objs, &e)
		}
	}
	return

}

//删除指定ID的活动
func EVENT_DelOne(id int) (aft int64) {
	link()
	defer close()

	sql_delete := fmt.Sprintf(`
	DELETE FROM event_calendar WHERE e_id = %d
	`, id)

	res, err := db.Exec(sql_delete)
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
	}

	aft, _ = res.RowsAffected()

	return

}

func EVENT_Update(obj EVENT) (aft int64) {
	link()
	defer close()

	sql_insert := `
	REPLACE INTO event_calendar
	(e_id,e_name,e_start,e_end,e_hash) VALUES
	(?,?,?,?,?)`

	stmt, err := db.Prepare(sql_insert)
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
		return 0
	}

	res, err := stmt.Exec(obj.E_id, obj.E_name, obj.E_start, obj.E_end, obj.HASHCODE())
	if err != nil {
		logger.GetLogger().PrintWarn(err.Error())
		return 0
	}
	aft, _ = res.RowsAffected()
	return
}
